﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Kaiser.Models.JsonModels;
using Kaiser.Modules.Mis.Services;
using Kaiser.Models;
using Kaiser.Modules.Mis.Models;
using System.Web.Script.Serialization;
using Kaiser.Extensions;
namespace Kaiser.Modules.Mis.Controllers
{
    public class BookManageController : BaseController
    {
        private readonly IBookManageService _bookManageService;
        public BookManageController(IBookManageService bookManageService)
        {
            this._bookManageService = bookManageService;
        }

        #region 图书管理

        public ActionResult EditBook()
        {
            return View();
        }

        public ActionResult DelCategorys(string ids)
        {
            String[] id = ids.Split(',');
            JsonReturnMessages result = new JsonReturnMessages(); ;
            if (!ids.IsNullOrWhiteSpace())
            {

                result.success = _bookManageService.DeleteCategoryList(ids) > 0;

                if (result.success)
                {
                    result.msg="删除成功";
                  
                }
                else
                {
                    result.msg = "删除失败";
                
                }
            }
            else
            {
                result.msg= "传值失败，请告诉开发者解决";
            }
            JavaScriptSerializer json = new JavaScriptSerializer();
            return Content(json.Serialize(result), "text/html;charset=UTF-8");
        }

        public ActionResult DelBooks(string ids)
        {
            String[] id = ids.Split(',');
            JsonReturnMessages result = new JsonReturnMessages(); ;
            if (!ids.IsNullOrWhiteSpace())
            {

                result.success = _bookManageService.DeleteBookList(ids) > 0;

                if (result.success)
                {
                    result.msg = "删除成功";

                }
                else
                {
                    result.msg = "删除失败";

                }
            }
            else
            {
                result.msg = "传值失败，请告诉开发者解决";
            }
            JavaScriptSerializer json = new JavaScriptSerializer();
            return Content(json.Serialize(result), "text/html;charset=UTF-8");
        }

        [HttpPost]
        public ActionResult EditBook(string id, BookInput model, FormCollection form)
        {
            JsonReturnMessages result = new JsonReturnMessages();

            try
            {

                if (id.IsNullOrWhiteSpace())
                {
                    _bookManageService.InsertBook(model);
                }
                else
                {
                    _bookManageService.UpdateBook(model);
                }
                result.success = true;
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result, "text/html");
        }

        [HttpPost]
        public ActionResult AddBook(BookInput model, FormCollection form)
        {
            return EditBook("", model, form);
        }
        public ActionResult BookList()
        {
            return View();
        }

        [HttpPost]
        public ActionResult BookList(BookSearch search, PagingInfo pageinfo, FormCollection form)
        {
            var result = this._bookManageService.SearchBook(search, pageinfo);
            return Content(result.ToJsonString());
        }

        #endregion

        #region 分类管理

        public ActionResult CategoryList()
        {
            return View();
        }
        [HttpPost]
        public ActionResult CategoryList(CategorySearch search, PagingInfo pageinfo, FormCollection form)
        {
            var result = this._bookManageService.SearchCategroy(search, pageinfo);
            return Content(result.ToJsonString());
        }

        [HttpPost]
        public ActionResult EditCategory(string id, CategoryInput model, FormCollection form)
        {
            JsonReturnMessages result = new JsonReturnMessages();

            try
            {
                if (id.IsNullOrWhiteSpace())
                {
                    _bookManageService.InsertCategory(model);
                }
                else
                {
                    _bookManageService.UpdateCategory(model);
                }
                result.success = true;
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result, "text/html");
        }

        [HttpPost]
        public ActionResult AddCategory(CategoryInput model, FormCollection form)
        {
            return EditCategory("", model, form);
        }

        [HttpPost]
        public ActionResult GetCategoryTree(CategorySearch search)
        {
            var pageinfo = new PagingInfo();
            pageinfo.Size = 10000;
            

            var result = _bookManageService.SearchCategroy(new CategorySearch(), pageinfo);
            JsonTree json = new JsonTree(result.TableResult, "CategoryName", "CategoryCode", "CategoryParentCode");
            json.RootList.Add(new JsonTreeNode() { id = search.CategoryParentCode, text = "分类目录" });
            return Content(json.ToJson(), "text/html");
        }
        #endregion

        #region 图书出版社
        public ActionResult PublisherList() {
            return View();
        }

        [HttpPost]
        public ActionResult GetBookPublisherList(PublisherSearch search)
        {
            var pageinfo = new PagingInfo();
            pageinfo.Size = 10;
            search.PublisherName = Request["q"];
            var result = _bookManageService.SearchPublisher(search, pageinfo);

            return Content(result.ToJsonString(), "text/html");
        }

        [HttpPost]
        public ActionResult EditPublisher(string id, PublisherInput model, FormCollection form)
        {
            JsonReturnMessages result = new JsonReturnMessages();

            try
            {
                if (id.IsNullOrWhiteSpace())
                {
                    _bookManageService.InsertPublisher(model);
                }
                else
                {
                    _bookManageService.UpdatePublisher(model);
                }
                result.success = true;
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result, "text/html");
        }

        [HttpPost]
        public ActionResult AddPublisher(PublisherInput model, FormCollection form)
        {
            return EditPublisher("", model, form);
        }

        public ActionResult DelPublishers(string ids)
        {
            String[] id = ids.Split(',');
            JsonReturnMessages result = new JsonReturnMessages(); ;
            if (!ids.IsNullOrWhiteSpace())
            {

                result.success = _bookManageService.DeletePublisherList(ids) > 0;

                if (result.success)
                {
                    result.msg = "删除成功";

                }
                else
                {
                    result.msg = "删除失败";

                }
            }
            else
            {
                result.msg = "传值失败，请告诉开发者解决";
            }
            JavaScriptSerializer json = new JavaScriptSerializer();
            return Content(json.Serialize(result), "text/html;charset=UTF-8");
        }

        #endregion
    }
}
